package com.chuxing.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuxing.domain.DTO.GetTimeListDTO;
import com.chuxing.domain.Time;
import com.chuxing.domain.VO.PageResultVO;
import com.chuxing.exception.GenericException;
import com.chuxing.system.mapper.TimeMapper;
import com.chuxing.system.service.ITimeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 考试及面试时间Service业务层处理
 * 
 * @author 刘祺
 * @date 2024-11-30
 */
@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class TimeServiceImpl extends ServiceImpl<TimeMapper, Time> implements ITimeService {

    private final TimeMapper timeMapper;

    /**
     * 查询考试及面试时间
     * 
     * @param id 考试及面试时间主键
     * @return 考试及面试时间
     */
    @Override
    @Cacheable(value = "time", key = "'id'+#id")
    public Time selectTimeById(Long id)
    {
        return timeMapper.selectTimeById(id);
    }

    /**
     * 查询考试及面试时间列表
     * 
     * @param getTimeListDTO 考试及面试时间
     * @return 考试及面试时间
     */

    @Override
    @Cacheable(value = "time", key = "#getTimeListDTO")
    public PageResultVO selectTimeList(GetTimeListDTO getTimeListDTO)
    {
        // 验证分页参数
        int pageNum = Math.max(1, getTimeListDTO.getPageNum());
        int pageSize = Math.max(1, getTimeListDTO.getPageSize());
        // 启用分页查询
        PageHelper.startPage(pageNum, pageSize);
        try {
            try (Page<Time> page = timeMapper.selectTimeList(getTimeListDTO)) {
                // 构造并返回分页结果对象
                return new PageResultVO(page.getTotal(), page.getResult());
            }
        } catch (Exception e) {
            // 记录异常日志
            log.error("获取用户列表时发生错误", e);
            // 抛出通用异常，不暴露具体错误信息
            throw new GenericException("系统内部错误");
        } finally {
            // 确保分页查询结束
            PageHelper.clearPage();
        }
    }

    /**
     * 新增考试及面试时间
     * 
     * @param time 考试及面试时间
     * @return 结果
     */
    @Override
    @CacheEvict(value = "time", allEntries = true)
    public void insertTime(Time time) {
        timeMapper.insertTime(time);
    }

    /**
     * 修改考试及面试时间
     * 
     * @param time 考试及面试时间
     * @return 结果
     */
    @Override
    @CacheEvict(value = "time", allEntries = true)
    public void updateTime(Time time)
    {
        timeMapper.updateTime(time);
    }

    /**
     * 批量删除考试及面试时间
     * 
     * @param ids 需要删除的考试及面试时间主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "time", allEntries = true)
    public void deleteTimeByIds(Long[] ids)
    {
        // 批量删除考试及面试时间
        timeMapper.deleteTimeByIds(ids);
    }
}
